package org.acb.sgp.factory;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConnectionFactory {

    private final String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private final String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=SGP;Trusted_Connection=True;";
    private final String dbUser = "sa";
    private final String dbPassword = "marco";

    private static ConnectionFactory connectionFactory = null;

    private ConnectionFactory() {
        try {
            Class.forName(driverClassName);
        } catch (ClassNotFoundException e) {
            System.out.println("Error en ConnectionFactory>ConnectionFactory: " + e.getMessage());
        }
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(connectionUrl, dbUser, dbPassword);
    }

    public static synchronized ConnectionFactory getInstance() {
        if (connectionFactory == null) {
            connectionFactory = new ConnectionFactory();
        }
        return connectionFactory;
    }

    public static void closing(ResultSet rs, CallableStatement cs, Connection con) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (cs != null) {
                cs.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
